package com.chart.web.controller.system;

import java.util.List;
import java.util.Set;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chart.common.annotation.Log;
import com.chart.common.core.domain.entity.SysDemand;
import com.chart.common.enums.BusinessType;
import com.chart.common.utils.StringUtils;
import com.chart.system.domain.SysUserOrder;
import com.chart.system.domain.SysUserPost;
import com.chart.system.domain.SysUserRole;
import com.chart.system.mapper.SysUserMapper;
import com.chart.system.mapper.SysUserPostMapper;
import com.chart.system.mapper.SysUserRoleMapper;
import com.chart.system.service.ISysUserDemandService;
import com.chart.system.service.ISysUserOrderService;
import com.chart.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.chart.common.constant.Constants;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.core.domain.entity.SysMenu;
import com.chart.common.core.domain.entity.SysUser;
import com.chart.common.core.domain.model.LoginBody;
import com.chart.common.utils.SecurityUtils;
import com.chart.framework.web.service.SysLoginService;
import com.chart.framework.web.service.SysPermissionService;
import com.chart.system.service.ISysMenuService;

import javax.annotation.Resource;

import static com.chart.common.core.domain.AjaxResult.error;

/**
 * 登录验证
 *
 * @author chart
 */
@Api(tags = "登陆模块")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserDemandService userDemandService;


    @Autowired
    private ISysUserOrderService userOrderService;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录方法")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @Resource
    private SysUserMapper userMapper;

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysDemand> demands = userDemandService.getDemandList(user.getUserId());
        user.setDemands(demands);
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        // Set<String> permissions = permissionService.getMenuPermission(user);

        SysUserOrder order = userOrderService.getOne(new LambdaQueryWrapper<SysUserOrder>().eq(SysUserOrder::getUserId, user.getUserId()));
        if (order != null) {
            if (order.getExpirationTime().isBefore(LocalDateTimeUtil.now())) {
                user.setIsVip(0);
                userMapper.updateById(user);
            } else {
                user.setExpirationTime(order.getExpirationTime());
            }
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
//        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

}
